package bairui.international.model.domain.jpa.id;

/**
 *
 */
@SuppressWarnings("serial")
@javax.persistence.Embeddable
@com.lianjing.sh.processing.info.annotation.GenerateInfoModel
@com.lianjing.sh.model.common.annotation.Comment("")
@com.lianjing.sh.processing.info.annotation.InfoComment("")
public class ID_IndexModuleProduct implements java.io.Serializable, com.lianjing.sh.model.common.stardard.CompositePrimaryKey {

	public ID_IndexModuleProduct(){}
 
	public ID_IndexModuleProduct(bairui.international.model.domain.jpa.TB_Product product, bairui.international.model.domain.jpa.TB_IndexModule indexModule) {
		this.product = product;
		this.indexModule = indexModule;
	}

	/**
	 * 属性
	 */
	
	@com.querydsl.core.annotations.QueryInit("*.*.*")
	@javax.persistence.ManyToOne(optional=false)
	private bairui.international.model.domain.jpa.TB_Product product;

	
	@com.querydsl.core.annotations.QueryInit("*.*.*")
	@javax.persistence.ManyToOne(optional=false)
	private bairui.international.model.domain.jpa.TB_IndexModule indexModule;
 
	/**
	 * 方法
	 */
	/** get[产品] */
	public bairui.international.model.domain.jpa.TB_Product getProduct() {
		return this.product;
	}

	/** set[产品] */
	public void setProduct(bairui.international.model.domain.jpa.TB_Product product) {
		this.product = product;
	}

	/** get[首页模块] */
	public bairui.international.model.domain.jpa.TB_IndexModule getIndexModule() {
		return this.indexModule;
	}

	/** set[首页模块] */
	public void setIndexModule(bairui.international.model.domain.jpa.TB_IndexModule indexModule) {
		this.indexModule = indexModule;
	}
	/**
	 * 复写 hashCode
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((product == null) ? 0 : product.hashCode());
		result = prime * result + ((indexModule == null) ? 0 : indexModule.hashCode());
		return result;
	}

	/**
	 * 复写 equals
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj){
			return true;
		}
		if (obj == null){
			return false;
		}
		if (getClass() != obj.getClass()){
			return false;
		}
		ID_IndexModuleProduct other = (ID_IndexModuleProduct) obj;
		if (product == null) {
			if (other.product != null){
				return false;
			}
		} else if (!product.equals(other.product)){
			return false;
		}
		if (indexModule == null) {
			if (other.indexModule != null){
				return false;
			}
		} else if (!indexModule.equals(other.indexModule)){
			return false;
		}
		return true;
	}

	/**
	 * 特征码，用于版本更新
	 */
	protected static final String SpecificCode = "product-indexModule";
	private static String Number;
	private static String Version;

	@Override
	public String toString() {
		return "(" + this.product.getId() + "," + this.indexModule.getId() + "," + Number + "-" + Version + ")";
	}

	/**
	 * 为Spring提供数据绑定转换
	 */
	public static org.springframework.core.convert.converter.Converter<String, ? extends ID_IndexModuleProduct> convert(){
		return new org.springframework.core.convert.converter.Converter<String, ID_IndexModuleProduct>() {
			@Override public ID_IndexModuleProduct convert(String source) {
				if(null == source){
					return null;
				}
				String[] arr = source
						.substring(source.indexOf("(")+1, source.lastIndexOf(")"))
						.split(",");
				// 验证版本
				String checkCode = arr[arr.length-1];
				String[] checkCodeArr = checkCode.split("-");
				if(!checkCodeArr[0].equals(Number) || !checkCodeArr[1].equals(Version)){
					throw new RuntimeException("复合主键类型版本错误");
				}
				// 转换
				return new ID_IndexModuleProduct(
						new bairui.international.model.domain.jpa.TB_Product(Long.parseLong(arr[0])), 
						new bairui.international.model.domain.jpa.TB_IndexModule(Long.parseLong(arr[1]))
					);
			}
		};
	}

	/**
	 * 复合主键自定义序列化
	 */
	public static class CompositePKSerializer extends com.fasterxml.jackson.databind.JsonSerializer<ID_IndexModuleProduct> {
		@Override public void serialize(ID_IndexModuleProduct value, com.fasterxml.jackson.core.JsonGenerator gen, com.fasterxml.jackson.databind.SerializerProvider serializers) throws java.io.IOException {
			gen.writeString(value.toString());
		}
	}


}
